package vn.hocweb.dao.dao_implement;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import vn.hocweb.dao.dao_interface.IUngDungDAO;
import vn.hocweb.entity.UngDung;

public class UngDungDAO extends GenericDAO implements IUngDungDAO{

	@Override
	public UngDung findByID(Long id) {
		Connection connection = getConnection();
		UngDung result = null;
		String sql = "select * from ungdung where maungdung = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, id);
			ResultSet effected = pre.executeQuery();
			if(effected.next()){
				long maUngDung = effected.getLong("maungdung");
				
				String ten = effected.getString("ten");
				String moTa = effected.getString("mota");
				String ghiChu = effected.getString("ghichu");
				int soLanXem = effected.getInt("solanxem");
				Date ngayCapNhat = effected.getDate("ngaycapnhat");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				long maBaiHoc = effected.getLong("mabaihoc");
				String nguoiCapNhat = effected.getString("nguoicapnhat");
				long maUngDung_LienQuan = effected.getLong("maungdung_lienquan");
				
				result = new UngDung(maUngDung, nguoiCapNhat, ten, moTa, 
						ghiChu, soLanXem, ngayCapNhat, maBaiHoc, maUngDung_LienQuan, 
						thuTu, noiBat, hienThi);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public List<UngDung> findAll(int firstItem, int batchSize) {
		Connection connection = getConnection();
		List<UngDung> result = new ArrayList<UngDung>();
		String sql = "select * from ungdung limit ?, ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, firstItem);
			pre.setLong(2, batchSize);
			ResultSet effected = pre.executeQuery();
			while (effected.next()){
				long maUngDung = effected.getLong("maungdung");
				
				String ten = effected.getString("ten");
				String moTa = effected.getString("mota");
				String ghiChu = effected.getString("ghichu");
				int soLanXem = effected.getInt("solanxem");
				Date ngayCapNhat = effected.getDate("ngaycapnhat");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				long maBaiHoc = effected.getLong("mabaihoc");
				String nguoiCapNhat = effected.getString("nguoicapnhat");
				long maUngDung_LienQuan = effected.getLong("maungdung_lienquan");
				
				UngDung row = new UngDung(maUngDung, nguoiCapNhat, ten, moTa, 
						ghiChu, soLanXem, ngayCapNhat, maBaiHoc, maUngDung_LienQuan, 
						thuTu, noiBat, hienThi);
				result.add(row);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public int size() {
		Connection connection = getConnection();
		int result = 0;
		String sql = "select count(*) from ungdung";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			ResultSet effected = pre.executeQuery();
			if(effected.next()){
				result = effected.getInt(0);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean save(UngDung entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "insert into ungdung (ten, mota, ghichu, solanxem, ngaycapnhat, " +
				"thutu, noibat, hienthi, mabaihoc, nguoicapnhat, maungdung_lienquan) " +
				"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setString(1, entity.getTen());
			pre.setString(2, entity.getMoTa());
			pre.setString(3, entity.getGhiChu());
			pre.setInt(4, entity.getSoLanXem());
			pre.setDate(5, entity.getNgayCapNhat());
			pre.setInt(6, entity.getThuTu());
			pre.setBoolean(7, entity.isNoiBat());
			pre.setBoolean(8, entity.isHienThi());
			pre.setLong(9, entity.getBaiHoc().getMaBaiHoc());
			pre.setString(10, entity.getNguoiCapNhat().getTenDangNhap());
			pre.setLong(11, entity.getUngDung_LienQuan().getMaBaiHoc());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean delete(UngDung entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "delete from ungdung where maungdung = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, entity.getMaBaiHoc());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean remove(UngDung entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "update ungdung set hienthi = ? where maungdung = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setBoolean(1, false);
			pre.setLong(2, entity.getMaBaiHoc());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean restore(UngDung entity) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "update ungdung set hienthi = ? where maungdung = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setBoolean(1, true);
			pre.setLong(2, entity.getMaBaiHoc());
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public List<UngDung> findAll_UngDungLienQuan(long maUD, int firstItem, int batchSize) {
		Connection connection = getConnection();
		List<UngDung> result = new ArrayList<UngDung>();
		String sql = "select * from ungdung where maungdung_lienquan = ? and maungdung_lienquan <> ? limit ?, ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, maUD);
			pre.setLong(2, maUD);
			pre.setInt(3, firstItem);
			pre.setInt(4, batchSize);
			ResultSet effected = pre.executeQuery();
			while (effected.next()){
				long maUngDung = effected.getLong("maungdung");
				
				String ten = effected.getString("ten");
				String moTa = effected.getString("mota");
				String ghiChu = effected.getString("ghichu");
				int soLanXem = effected.getInt("solanxem");
				Date ngayCapNhat = effected.getDate("ngaycapnhat");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				long maBaiHoc = effected.getLong("mabaihoc");
				String nguoiCapNhat = effected.getString("nguoicapnhat");
				long maUngDung_LienQuan = effected.getLong("maungdung_lienquan");
				
				UngDung row = new UngDung(maUngDung, nguoiCapNhat, ten, moTa, 
						ghiChu, soLanXem, ngayCapNhat, maBaiHoc, maUngDung_LienQuan, 
						thuTu, noiBat, hienThi);
				result.add(row);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public List<UngDung> findAll_danhSachUngDung(long maBH, int firstItem,
			int batchSize) {
		Connection connection = getConnection();
		List<UngDung> result = new ArrayList<UngDung>();
		String sql = "select * from ungdung where mabaihoc = ? limit ?, ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, maBH);
			pre.setInt(2, firstItem);
			pre.setInt(3, batchSize);
			ResultSet effected = pre.executeQuery();
			while (effected.next()){
				long maUngDung = effected.getLong("maungdung");
				
				String ten = effected.getString("ten");
				String moTa = effected.getString("mota");
				String ghiChu = effected.getString("ghichu");
				int soLanXem = effected.getInt("solanxem");
				Date ngayCapNhat = effected.getDate("ngaycapnhat");
				int thuTu = effected.getInt("thutu");
				boolean noiBat = effected.getBoolean("noibat");
				boolean hienThi = effected.getBoolean("hienthi");
				
				long maBaiHoc = effected.getLong("mabaihoc");
				String nguoiCapNhat = effected.getString("nguoicapnhat");
				long maUngDung_LienQuan = effected.getLong("maungdung_lienquan");
				
				UngDung row = new UngDung(maUngDung, nguoiCapNhat, ten, moTa, 
						ghiChu, soLanXem, ngayCapNhat, maBaiHoc, maUngDung_LienQuan, 
						thuTu, noiBat, hienThi);
				result.add(row);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

}
